namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        private void button1_Click(object sender, EventArgs e)
        {

            if (graphics == null)
            {
                graphics = this.CreateGraphics();
            }
            graphics.Clear(this.BackColor);
            drawCayleyTree(int.Parse(textBox1.Text), double.Parse(textBox2.Text), double.Parse(textBox3.Text), double.Parse(textBox4.Text), -Math.PI / 2);
            if (comboBox1.SelectedIndex == 1)
            {
                newpen = Pens.Blue;
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                newpen = Pens.Yellow;
            }

        }

        Pen newpen = new Pen(Color.Red);

        void drawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            if (n == 0) return;
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);
            drawLine(x0, y0, x1, y1);
            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(newpen, (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}